home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-11-29 | 8.1 KB | 393 lines | [TEXT/MPS ] |
- // ©1992 Conrad Carlen & Manuel Veloso. All rights reserved.
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
- #ifndef __EXCEPTIONS__
- #include <Exceptions.h>
- #endif
-
- #ifndef __APPLETALK__
- #include <AppleTalk.h>
- #endif
-
- #ifndef __ERRMGR__
- #include <ErrMgr.h>
- #endif
-
- #ifndef __Fonts__
- #include <Fonts.h>
- #endif
-
- #ifndef __CURSORCTL__
- #include <CursorCtl.h>
- #endif
-
- #ifndef __STRINGS__
- #include <Strings.h>
- #endif
-
- #ifndef __CTYPE__
- #include <ctype.h>
- #endif
-
- #ifndef __STDLIB__
- #include <stdlib.h>
- #endif
-
- #ifndef __IOSTREAM__
- #include <iostream.h>
- #endif
-
- #ifndef __FSTREAM__
- #include <fstream.h>
- #endif
-
- #ifndef __IOMANIP__
- #include <iomanip.h>
- #endif
-
- #ifndef _IDLE_
- #include "idle.h"
- #endif
-
- #ifndef _PAPFILE_
- #include "Papfile.h"
- #endif
-
- #ifndef _PAPTOOL_
- #include "Paptool.h"
- #endif
-
- #ifndef _PAPPATCH_
- #include "Pappatch.h"
- #endif
-
- #ifndef _PREPROCESS_
- #include "Preprocess.h"
- #endif
-
- char *copyright = "©1992 Conrad Carlen & Manuel Veloso. All rights reserved.";
-
- CPapFile *theFile = nil;
-
- ofstream out; // globalized
- ifstream input; // globalized because of error: << sorry, not implemented: label in block with destructors >>
- Boolean gStdin = false; // set to true if stdin is the file
- char errMsg[255]; // global error msg buffer
- Boolean gPatchInstalled;// set to true if patch was installed.
-
- #pragma segment Initialize
- void initTool(void)
- {
- SetFScaleDisable(true);
- InitCursorCtl(nil);
- InitErrMgr(nil, nil, true);
- }
-
- #pragma segment Initialize
- void usage(void)
- {
- out << "### Usage: PAPTool -n ObjectName -t ObjectType [-z ObjectZone] [files…]" << endl << endl;
- }
-
- #pragma segment Initialize
- OSErr extractName(short argc, char *argv[], short &files, EntityName &thePrinter)
- {
- short parms;
- short length;
- Str32 objStr, typeStr, zoneStr;
- Boolean name = false, type = false, zone = false;
-
- for (parms = 1; parms < argc; parms++)
- {
- length = strlen(argv[parms]);
- if (*argv[parms] != '-') files++;
- else if (tolower(*(argv[parms] + 1)) == 'n' && length == 2)
- {
- argv[parms] = 0;
- strcpy((char *) &objStr[0], argv[++parms]);
- argv[parms] = 0;
- name = true;
- }
- else if (tolower(*(argv[parms] + 1)) == 't' && length == 2)
- {
- argv[parms] = 0;
- strcpy((char *) &typeStr[0], argv[++parms]);
- argv[parms] = 0;
- type = true;
- }
- else if (tolower(*(argv[parms]+1)) == 'z' && length == 2)
- {
- argv[parms] = 0;
- strcpy((char *) &zoneStr[0], argv[++parms]);
- argv[parms] = 0;
- zone = true;
- }
- else
- {
- out << "### " << argv[0] << " " << argv[parms] << " is not an option." << endl;
- usage();
- return 1;
- }
- }
-
- if (!name)
- {
- out << "### " << argv[0] << " Error: no name was specified." << endl;
- usage();
- return 1;
- }
-
- if (!type)
- {
- out << "### " << argv[0] << " Error: no type was specified." << endl;
- usage();
- return 1;
- }
-
- if (!zone)
- {
- strcpy((char *) &zoneStr[0], "*");
- }
-
- c2pstr((char *) &objStr[0]);
- c2pstr((char *) &typeStr[0]);
- c2pstr((char *) &zoneStr[0]);
- NBPSetEntity((Ptr) &thePrinter, (Ptr) &objStr[0], (Ptr) &typeStr[0], (Ptr) &zoneStr[0]);
- return 0;
- }
-
- #pragma segment Main
- void cleanup(void)
- {
- long myA5 = myGetA5();
- long oldA5 = SetA5(myA5);
-
- if (theFile)
- {
- delete theFile; // note: theFile->Close checks for non-open files,
- }
- theFile = nil; // doing this isn't a problem
- if (gPatchInstalled)
- {
- removePatch();
- }
- SetA5(oldA5);
- }
-
- #pragma segment Main
- short main(short argc, char *argv[])
- {
- OSErr error;
- short numFiles = 0, i;
- EntityName targetPrinter;
-
- initTool();
-
- out.attach(1); // attach out to stdout
-
- nrequire_action(out.fail(), out_attach_1, cerr << "### Unable to attach to stdout!" << endl;);
-
- out.setf(1, ios::unitbuf); // flush after each lf
- error = extractName(argc, argv, numFiles, targetPrinter);
- nrequire_action(error, extract_name, out << "### Unable to extract name!" << endl;);
-
- theFile = new CPapFile;
- error = theFile->IPapFile();
- nrequire_action(error, thefile_ipapfile, out << "### Unable to create CPapFile!" << endl;); // no creation of pap object!
-
- error = openPrinter(targetPrinter);
- nrequire(error, open_printer);
-
- atexit(cleanup);
-
- error = installPatch(); // installs the hooks for PAPRead! Removed @exit
- require_action(error, install_patch, out << "### Unable to install read patch!" << endl;);
-
- error = DoIt(argc, argv, numFiles, input, out);
- nrequire(error, doit);
-
- theFile->Close();
- out << "### Printer closed. ###" << endl;
- return error;
- doit:
- install_patch:
- theFile->Close();
- open_printer:
- thefile_ipapfile:
- extract_name:
- out << "### " << " Error processing file " << argv[0] << endl;
- if (error < 0)
- {
- out << "### " << GetSysErrText(error, errMsg) << endl;
- }
- out_attach_1:
- return 1;
- }
-
- #pragma segment Initialize
- OSErr openPrinter(EntityName &thePrinter)
- {
- OSErr error;
- error = theFile->Open(thePrinter);
- if (error >= 0)
- {
- while (theFile->GetOpenStatus() > 0)
- {
- idle();
- }
- error = theFile->GetLastReadErr();
- }
-
- nrequire_action
- ((((theFile->GetOpenStatus() < 0)) || (error != 0)), thefile_open,
- (
- out
- << "### Unable to open printer! Name: "
- << p2cstr(theFile->GetPrinterName())
- << " Type: " << p2cstr(theFile->GetPrinterType())
- << " Zone: " << p2cstr(theFile->GetPrinterZone())
- << " ! ###" << endl
- );
- );
- out << endl << "### The printer has been opened. ###" << endl;
- thefile_open:
- return error;
- }
-
- #pragma segment Main
- short getData(char *theBuffer, short bufferSize, ifstream &input)
- {
- short count;
- if (gStdin)
- {
- input.getline(theBuffer, bufferSize);
- count = input.gcount();
- count = preprocessWrite(theBuffer, count);
- }
- else
- {
- input.read(theBuffer, bufferSize);
- count = input.gcount();
- }
- return count;
- }
-
- #pragma segment Main
- OSErr DoIt(short argc, char *argv[], short numFiles, ifstream &input, ofstream &output)
- {
- short i;
- OSErr error;
- if (numFiles == 0)
- {
- gStdin = true;
- input.attach(0);
- nrequire_action(input.fail(), input_attach, out << "### Unable to attach to stdin!" << endl;);
-
- input.setf(0, ios::skipws); // don’t skip whitespace
- out << "### Ready for input. ###" << endl;
- error = processFile(input, *theFile);
- nrequire_action(error, process_file_1, out << "### ProcessFile error!" << endl;);
- }
- else
- {
- gStdin = false;
- for (i = 1; (i < argc) && numFiles; i++)
- {
- if (argv[i])
- {
- numFiles--;
- input.open(argv[i]);
- nrequire_action(input.fail(), input_open, out << "### Unable to open file: " << argv[1] << " ###" << endl;);
-
- input.setf(0, ios::skipws); // don’t skip whitespace. It doesn’t matter here, tho
- out << "### Processing file " << argv[i] << " ###" << endl;
- error = processFile(input, *theFile);
- nrequire_action(error, process_file_2, out << "### ProcessFile error! ###" << endl;);
-
- input.close();
- }
- }
- }
- return error;
- process_file_1:
- input.close();
- input_attach:
- return error;
- process_file_2:
- input.close();
- input_open:
- return error;
- }
-
- #pragma segment Main
- OSErr processFile(ifstream &input, CPapFile &theFile)
- {
- OSErr error = noErr;
- short bufferSize = theFile.GetWriteSize();
- char *bufferAddr = theFile.GetWriteBufferAddress();
- int count;
-
- while (!input.eof())
- {
- count = getData(bufferAddr, bufferSize, input);
- theFile.SetWriteCount(count);
- error = theFile.Write();
- if (error) break;
-
- if (!out.fail())
- {
- while (theFile.GetWriteStatus() > 0)
- {
- idle();
- }
- }
- if (error = theFile.GetLastWriteErr()) break;
- }
- return error;
- }
-
- #pragma segment Initialize
- Boolean installPatch(void)
- {
- Install(checkRead, SetCurrentA5());
- gPatchInstalled = true;
- }
-
- #pragma segment Initialize
- Boolean removePatch(void)
- {
- Remove();
- gPatchInstalled = false;
- }
-
- #pragma segment Main
- void checkRead(void)
- {
- long myA5 = myGetA5();
- long oldA5 = SetA5(myA5);
- OSErr error = theFile->GetLastReadErr();
-
- if (error)
- {
- out << "### Read error!" << GetSysErrText(error, errMsg) << " ###" << endl;
- out << "### Terminating! ###" << endl;
- SetA5(oldA5);
- exit(1);
- }
- if (theFile->GetReadStatus() <= 0)
- {
- short count = theFile->GetReadCount();
- char *buffer = theFile->GetReadBufferAddress();
-
- count = preprocessRead(buffer, count, theFile->GetWriteBufferAddress());
- out.write(buffer, count);
- out.flush();
- theFile->Read();
- }
- SetA5(oldA5);
- return;
- }
-